package cn.ujn.medicalbackend.service.impl;

import cn.ujn.medicalbackend.common.ErrorCode;
import cn.ujn.medicalbackend.domain.CompanyPolicy;
import cn.ujn.medicalbackend.exception.BusinessException;
import cn.ujn.medicalbackend.mapper.CompanyPolicyMapper;
import cn.ujn.medicalbackend.request.CompanyPolicyAddRequest;
import cn.ujn.medicalbackend.request.CompanyPolicyDeleteRequest;
import cn.ujn.medicalbackend.request.CompanyPolicyUpdateRequest;
import cn.ujn.medicalbackend.service.CompanyPolicyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
* @author 15926
* @description 针对表【company_policy】的数据库操作Service实现
* @createDate 2024-10-26 14:41:20
*/
@Service
public class CompanyPolicyServiceImpl extends ServiceImpl<CompanyPolicyMapper, CompanyPolicy>
    implements CompanyPolicyService {
    @Override
    public Page<CompanyPolicy> companyPolicyPageTransfer(Page<CompanyPolicy> tempPage) {
        return tempPage;
    }

    @Override
    public long addCompanyPolicy(CompanyPolicyAddRequest companyPolicyAddRequest, HttpServletRequest request) {
        CompanyPolicy companyPolicy = new CompanyPolicy();
        BeanUtils.copyProperties(companyPolicyAddRequest, companyPolicy);
        System.out.println(companyPolicy.toString());
        Date now = new Date();
        companyPolicy.setCreateTime(now);
        companyPolicy.setUpdateTime(now);
        // 3.校验信息
        if (companyPolicy.getTitle() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "不能为空");
        }
        boolean result;
        result = save(companyPolicy);
        if (!result) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "保存失败");
        }

        return companyPolicy.getId();
    }

    @Override
    public boolean updateCompanyPolicy(CompanyPolicyUpdateRequest companyPolicyUpdateRequest, HttpServletRequest request) {
        if (companyPolicyUpdateRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Date now = new Date();
        CompanyPolicy companyPolicy =getById(companyPolicyUpdateRequest.getId());
        if(companyPolicy==null){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "该公司政策不存在，更新失败");
        }
        BeanUtils.copyProperties(companyPolicyUpdateRequest, companyPolicy);
        companyPolicy.setUpdateTime(now);

        boolean result =updateById(companyPolicy);
        if (!result) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "更新失败");
        }

        return true;
    }

    @Override
    public boolean deleteCompanyPolicy(CompanyPolicyDeleteRequest companyPolicyDeleteRequest, HttpServletRequest request) {
        // 鉴权，只有管理员可创建
        Long companyPolicyId = companyPolicyDeleteRequest.getId();

        if (companyPolicyId == null ) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "；公司政策id和题目不能为空");
        }
        QueryWrapper<CompanyPolicy> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", companyPolicyId);
        long count = this.count(queryWrapper);
        if (count <= 0) {
            throw new BusinessException(ErrorCode.NULL_ERROR, "没有该公司政策");
        }

        boolean result = removeById(companyPolicyId);
        if (!result) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "删除失败");
        }
        return true;
    }
}




